//
//  ContactsViewController.m
//  MFWebBrowser
//
//  Created by Dream on 2018/7/26.
//  Copyright © 2018年 Dream. All rights reserved.
//

#import "ContactsViewController.h"
#import "ContactsSuccessViewController.h"
#import "ContactsFailViewController.h"
#import "UIImage+Contact.h"
#import "ContactsUtil.h"

@interface ContactsViewController ()

@end

@implementation ContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //布局UI
    [self doInitView];
}

#pragma mark - 布局UI
- (void)doInitView
{
    self.navigationItem.title = @"通讯录认证";
    self.view.backgroundColor = [UIColor whiteColor];
    
    //屏幕宽高
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    
    //提示
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSBundle *resourcesBundle = [NSBundle bundleWithPath:[bundle pathForResource:@"ContactsResources" ofType:@"bundle"]];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_book" inBundle:resourcesBundle compatibleWithTraitCollection:nil]];
    imageView.center = CGPointMake(screenWidth * 0.5, 130);
    [self.view addSubview:imageView];
    
    
    //提示
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame) + 30, screenWidth, 30)];
    titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"需要获取你的通讯录权限";
    [self.view addSubview:titleLabel];
    
    //副标题
    UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame), screenWidth, 40)];
    subLabel.font = [UIFont systemFontOfSize:14.0];
    subLabel.textColor = [UIColor lightGrayColor];
    subLabel.textAlignment = NSTextAlignmentCenter;
    subLabel.numberOfLines = 0;
    subLabel.text = @"请点击以下按钮，提交你的手机通讯录以完成认证";
    [self.view addSubview:subLabel];
    
    //按钮
    UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(15, screenHeight - 100, screenWidth - 30, 44)];
    
    startButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [startButton setTitle:@"提交通讯录认证" forState:UIControlStateNormal];
    
    
    [startButton setBackgroundImage:[UIImage createImageWithColor:[[UIColor redColor] colorWithAlphaComponent:0.7]] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage createImageWithColor:[UIColor redColor]] forState:UIControlStateHighlighted];
    
    [startButton addTarget:self action:@selector(startAuthAction) forControlEvents:UIControlEventTouchUpInside];
    
    startButton.layer.cornerRadius = 22;
    startButton.layer.masksToBounds = YES;
    
    [self.view addSubview:startButton];
}

#pragma mark - 通讯录认证
- (void)startAuthAction
{
    [ContactsUtil getUserAllContacts:^(NSArray *contacts, NSError *error) {
      
        if (!error) {
            
            if (contacts.count > 0) {
                
                if (self.ContactsBlock) {
                    
                    self.ContactsBlock(contacts);
                }
                
                //认证成功界面
                ContactsSuccessViewController *vc = [[ContactsSuccessViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else {
                
                //通讯录为空 跳转认证失败界面
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    //跳转认证失败界面
                    ContactsFailViewController *vc = [[ContactsFailViewController alloc] init];
                    [self.navigationController pushViewController:vc animated:YES];
                });
            }
        }
        else {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //跳转认证失败界面
                ContactsFailViewController *vc = [[ContactsFailViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            });
        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
